package com.fy.servlet;

import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import com.fy.model.Page;
import com.fy.model.Student;
import com.fy.util.DbUtil;

@WebServlet("/students")
public class StudentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String methodName = request.getParameter("methodName");
		int method = Integer.parseInt(methodName);
		try {
			switch (method) {
				case -1:
					insertui(request, response);
				case 0:
					insert(request, response);
				case 1:
					difpage(request, response);
					break;
				case 2:
					delete(request, response);
					break;
				case 3:
					update(request, response);
					break;
				case 4:
					updateui(request, response);
					break;
				case 5:
					dispatch(request, response);
					break;
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	// 插入方法
	public void insertui(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException,
			SQLException, ServletException, IOException {
		request.getRequestDispatcher("/WEB-INF/page/student/add.jsp").forward(request, response);
	}
	public void insert(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException,
			SQLException {
		Connection conn = DbUtil.getMyConn();
		Statement state = conn.createStatement();
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String age = request.getParameter("age");
		String gender = request.getParameter("gender");
		String major = request.getParameter("major");
		state.execute("insert into student(id,name,age,gender,major) values(" + id + ",'" + name + "'," + age + ",'"
				+ gender + "','" + major + "')");
		DbUtil.close(conn, state, null);
	}
	// 信息删除方法
	public void delete(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException,
			SQLException, ServletException, IOException {
		Connection conn = DbUtil.getMyConn();
		Statement state = conn.createStatement();
		String id = request.getParameter("id");
		state.execute("delete from student where id=" + id + "");
		request.getRequestDispatcher("/WEB-INF/page/student/delete.jsp").forward(request, response);
	}
	// 信息修改方法
	public void updateui(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException,
			SQLException, ServletException, IOException {
		String id = request.getParameter("id");
		request.setAttribute("result", select(id, ""));
		request.getRequestDispatcher("/WEB-INF/page/student/updateui.jsp").forward(request, response);
	}
	public void update(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException,
			SQLException, ServletException, IOException {
		Connection conn = DbUtil.getMyConn();
		Statement state = conn.createStatement();
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String age = request.getParameter("age");
		String gender = request.getParameter("gender");
		String major = request.getParameter("major");
		state.execute("update student set id=" + id + ",name='" + name + "',age=" + age + ",gender='" + gender
				+ "',major='" + major + "' where id=" + id + "");
		request.setAttribute("result", select(id, ""));
		request.getRequestDispatcher("/WEB-INF/page/student/update.jsp").forward(request, response);
	}
	// 查询方法
	public ArrayList<Student> select(String id, String name) throws ClassNotFoundException, SQLException {
		Connection conn = DbUtil.getMyConn();
		Statement state = conn.createStatement();
		ResultSet rs = null;
		ArrayList<Student> result = new ArrayList<Student>();
		if (id == "" && name == "")
			rs = state.executeQuery("select * from student");
		if (id != "" && name == "")
			rs = state.executeQuery("select * from student where id=" + id + "");
		if (id == "" && name != "")
			rs = state.executeQuery("select * from student where name='" + name + "'");
		if (id != "" && name != "")
			rs = state.executeQuery("select * from student where id=" + id + " and name='" + name + "'");
		while (rs.next()) {
			Student st = new Student();
			st.setId(rs.getInt("id"));
			st.setName(rs.getString("name"));
			st.setAge(rs.getInt("age"));
			st.setGender(rs.getString("gender"));
			st.setMajor(rs.getString("major"));
			result.add(st);
		}
		if (rs != null)
			rs.close();
		DbUtil.close(conn, state, null);
		return result;
	}
	// 条件查询跳转
	public void dispatch(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException,
			SQLException, ServletException, IOException {
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		if (select(id, name).isEmpty())
			request.getRequestDispatcher("/WEB-INF/page/student/notfound.jsp").forward(request, response);
		else {
			request.setAttribute("result", select(id, name));
			request.getRequestDispatcher("/WEB-INF/page/student/found.jsp").forward(request, response);
		}
	}
	// 设置分页相关参数方法
	public Page setpage(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException,
			SQLException {
		String crd = request.getParameter("currentRecord");
		ArrayList<Student> result = select("", "");
		Page pager = new Page();
		pager.setTotalRecord(result.size());
		pager.setTotalPage(result.size(), pager.getPageSize());
		if (crd != null) {
			int currentRecord = Integer.parseInt(crd);
			pager.setCurrentRecord(currentRecord);
			pager.setCurrentPage(currentRecord, pager.getPageSize());
		}
		return pager;
	}
	// 获得分页显示的子集
	public void difpage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException,
			ClassNotFoundException, SQLException {
		ArrayList<Student> result = select("", "");
		Page pager = new Page();
		pager = setpage(request, response);
		List<Student> subResult = null;
		int currentRecord = pager.getCurrentRecord();
		if (currentRecord == 0) {
			if (pager.getTotalRecord() < 8)
				subResult = result.subList(0, pager.getTotalRecord());
			else
				subResult = result.subList(0, pager.getPageSize());
		} else if (pager.getCurrentRecord() + pager.getPageSize() < result.size())
			subResult = result.subList(pager.getCurrentRecord(), pager.getCurrentRecord() + pager.getPageSize());
		else
			subResult = result.subList(pager.getCurrentRecord(), result.size());
		request.setAttribute("pager", pager);
		request.setAttribute("subResult", subResult);
		request.getRequestDispatcher("/WEB-INF/page/student/list.jsp").forward(request, response);
	}
}